package com.yinp.proapp.activity;

import android.text.TextUtils;
import android.view.View;

import com.yinp.proapp.activity.bean.MyUserInfo;
import com.yinp.proapp.base.activity.AppBaseActivity;
import com.yinp.proapp.constant.SpConstants;
import com.yinp.proapp.databinding.ActivityRegisterBinding;
import com.yinp.proapp.utils.SpUtils;
import com.yinp.proapp.utils.StatusBarUtil;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * @Author: yinp
 * @Date: 2021/11/22
 * @Description:注册界面
 */
public class RegisterActivity extends AppBaseActivity<ActivityRegisterBinding> {

    @Override
    protected void initViews() {
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(mContext));
        initClick(bd.stvRegister, bd.header.headerBackImg);
        bd.header.headerCenterTitle.setText("注册");

    }

    @Override
    public void onClick(View v) {
        if (v == bd.stvRegister) {
            register();
        } else if (v == bd.header.headerBackImg) {
            finish();
        }
    }

    private void register() {
        if (TextUtils.isEmpty(bd.tieAccount.getText())) {
            showToast("还没有填写账号");
            return;
        }
        if (TextUtils.isEmpty(bd.tiePassword.getText())) {
            showToast("还没有填写没密码");
            return;
        }
        if (TextUtils.isEmpty(bd.tiePasswordAgain.getText())) {
            showToast("还没有确认没密码");
            return;
        }
        String account = bd.tieAccount.getText().toString().trim();
        String password = bd.tiePassword.getText().toString().trim();
        String passwordAgain = bd.tiePasswordAgain.getText().toString().trim();
        if (!password.equals(passwordAgain)) {
            showToast("两次密码不一致");
            return;
        }
        MyUserInfo myUserInfo = new MyUserInfo();
        myUserInfo.setUsername(account);
        myUserInfo.setPassword(password);
        myUserInfo.setHeadUrl("https://s3.bmp.ovh/imgs/2021/11/31200f89109b91a7.jpg");
        myUserInfo.signUp(new SaveListener<MyUserInfo>() {
            @Override
            public void done(MyUserInfo myUserInfo, BmobException e) {
                if (e == null) {
                    SpUtils.saveValue(myUserInfo.getObjectId(), SpConstants.OBJECT_ID);
                    goToActivity(MajorActivity.class);
                    finish();
                } else {
                    switch (e.getErrorCode()) {
                        case 202:
                            showToast("注册的账号已经存在");
                            break;
                        default:
                            showToast("注册失败");
                    }
                }
            }
        });
    }
}